-- stdpath('config') returns $HOME/.config/nvim
-- NOTE: strange rules for neovim `packadd` method, only paths match 'pack/*/start/{dirname}' or 'pack/*/opt/{dirname}' can be added
-- local packer_install_dir = '~/.config/nvim/pack/packer/start/packer.nvim'
local packer_install_path = vim.fn.stdpath('config') .. '/pack/packer/start/packer.nvim'
local packer_pkg_root = vim.fn.stdpath('config') .. '/pack'
local packer_cache = vim.fn.stdpath('cache') .. '/packer.nvim'
local packer_compile = vim.fn.stdpath('config') .. '/pack/packer_complied.lua'

local packer_conf = {
  ensure_dependencies = true, -- Should packer install plugin dependencies?
  snapshot = nil, -- Name of the snapshot you would like to load at startup
  snapshot_path = packer_cache, -- Default save directory for snapshots
  package_root = packer_pkg_root,
  compile_path = packer_compile,
  plugin_package = 'packer', -- The default package for plugins
  max_jobs = nil, -- Limit the number of simultaneous jobs. nil means no limit
  auto_clean = true, -- During sync(), remove unused plugins
  compile_on_sync = true, -- During sync(), run packer.compile()
  disable_commands = false, -- Disable creating commands
  opt_default = false, -- Default to using opt (as opposed to start) plugins
  transitive_opt = true, -- Make dependencies of opt plugins also opt by default
  transitive_disable = true, -- Automatically disable dependencies of disabled plugins
  auto_reload_compiled = true, -- Automatically reload the compiled file after creating it.
  preview_updates = false, -- If true, always preview updates before choosing which plugins to update, same as `PackerUpdate --preview`.
  git = {
    cmd = 'git', -- The base command for git operations
    subcommands = { -- Format strings for git subcommands
      update         = 'pull --ff-only --progress --rebase=false',
      install        = 'clone --depth %i --no-single-branch --progress',
      fetch          = 'fetch --depth 999999 --progress',
      checkout       = 'checkout %s --',
      update_branch  = 'merge --ff-only @{u}',
      current_branch = 'branch --show-current',
      diff           = 'log --color=never --pretty=format:FMT --no-show-signature HEAD@{1}...HEAD',
      diff_fmt       = '%%h %%s (%%cr)',
      get_rev        = 'rev-parse --short HEAD',
      get_msg        = 'log --color=never --pretty=format:FMT --no-show-signature HEAD -n 1',
      submodules     = 'submodule update --init --recursive --progress'
    },
    depth = 1, -- Git clone depth
    clone_timeout = 600, -- Timeout, in seconds, for git clones
    -- default_url_format = 'https://github.com/%s', -- Lua format string used for "aaa/bbb" style plugins
    -- default_url_format = 'git@gitee.com:%s.git',
    -- default_url_format = 'https://gitcode.net/%s',
    default_url_format = '%s.git',
  },

  display = {
    open_fn = function()
      return require("packer.util").float({ border = "rounded" })
    end,
  },

  luarocks = {
    python_cmd = 'python' -- Set the python command to use for running hererocks
  },

  log = { level = 'warn' }, -- The default print log level. One of: "trace", "debug", "info", "warn", "error", "fatal".

  profile = {
    enable = false,
    threshold = 1, -- integer in milliseconds, plugins which load faster than this won't be shown in profile output
  },

  autoremove = false, -- Remove disabled or unused plugins without prompting the user
}

local ensure_packer = function()
  if vim.fn.empty(vim.fn.glob(packer_install_path)) > 0 then
    vim.fn.system({'git', 'clone', '--depth', '1', 'https://gitcode.net/nvim-packer/packer.nvim', packer_install_path})
    vim.cmd('packadd packer.nvim')
    return true
  end
  return false
end

local packer_bootstrap = ensure_packer()

vim.cmd([[
  augroup packer_user_config
    autocmd!
    autocmd BufWritePost pkg.lua source <afile> | PackerSync
  augroup end
]])

local ok, packer = pcall(require, 'packer')
if not ok then
  print('packer loaded error')
  return
end

packer.init(packer_conf)

return packer.startup(function(use)
    use({
      'https://gitcode.net/nvim-packer/packer.nvim',
    })

    use({
      'https://gitcode.net/nvim-packer/plenary.nvim',
    })

    use({
      'https://gitcode.net/nvim-packer/nvim-web-devicons'
    })

    use({
      'https://gitcode.net/nvim-packer/telescope.nvim',
      requires = { {'https://gitcode.net/nvim-packer/plenary.nvim'} },
    })

    use({
      'https://gitcode.net/nvim-packer/project.nvim',
      config = function()
        require('project_nvim').setup {
          -- your configuration comes here
          -- or leave it empty to use the default settings
          -- refer to the configuration section below
          sync_root_with_cwd = true,
          respect_buf_cwd = true,
          update_focused_file = {
            enable = true,
            update_root = true
          },
        }
      end
    })

    use({
      'https://gitcode.net/nvim-packer/indent-blankline.nvim',
    })

    use({
      'https://gitcode.net/nvim-packer/alpha-nvim',
      requires = { {'https://gitcode.net/nvim-packer/nvim-web-devicons'} },
    })


    use({
      'https://gitcode.net/nvim-packer/nvim-lspconfig',
    })

    use({
      'https://gitcode.net/nvim-packer/mason.nvim',
    })

    use({
      'https://gitcode.net/nvim-packer/mason-lspconfig.nvim',
    })

    use({
      'https://gitcode.net/nvim-packer/lspsaga.nvim',
      branch = 'main',
    })

    use({
      'https://gitcode.net/nvim-packer/lualine.nvim',
    })

	  use({
      'https://gitcode.net/nvim-packer/bufferline.nvim',
    })

    use({
      'https://gitcode.net/nvim-packer/nvim-tree.lua',
    })

    use({
      'https://gitcode.net/nvim-packer/nvim-treesitter',
    })

    use({
      'https://gitcode.net/nvim-packer/hop.nvim',
    })

    use({
      'https://gitcode.net/nvim-packer/nvim-solarized-lua',
    })

    use({
      'https://gitcode.net/nvim-packer/go.nvim',
    })

    use({
      'https://gitcode.net/nvim-packer/rust-tools.nvim',
      ft = 'rust',
      config = function()
        require('rust-tools').setup({
        })
      end,
    })

    use({
      'https://gitcode.net/nvim-packer/nvim-ts-rainbow',
      requires = { {'https://gitcode.net/nvim-packer/nvim-treesitter'} },
    })

    use({
      'https://gitcode.net/nvim-packer/nvim-surround',
      config = function()
        require('nvim-surround').setup({
        })
      end
    })

    use({
      'https://gitcode.net/nvim-packer/nvim-autopairs',
      config = function()
        require("nvim-autopairs").setup({
        })
      end
    })

    use({
      'https://gitcode.net/nvim-packer/git-blame.nvim',
      -- config = function()
      --   require('gitblame').setup({
      --   })
      -- end
    })

	  use({ "https://gitcode.net/nvim-packer/nvim-cmp", }) -- The completion plugin
	  use({ "https://gitcode.net/nvim-packer/cmp-buffer",}) -- buffer completions
	  use({ "https://gitcode.net/nvim-packer/cmp-path", }) -- path completions
	  use({ "https://gitcode.net/nvim-packer/cmp_luasnip", }) -- snippet completions
	  use({ "https://gitcode.net/nvim-packer/cmp-nvim-lsp", })
	  use({ "https://gitcode.net/nvim-packer/cmp-nvim-lua", })

    use({
      'https://gitcode.net/nvim-packer/Comment.nvim',
      config = function()
        require('Comment').setup()
      end
    })

    -- snippets
    use({
      'https://gitcode.net/nvim-packer/LuaSnip',
      tag = "v1.*",
    })

    use({
      'https://gitcode.net/nvim-packer/friendly-snippets',
    })

    use({
      'https://gitcode.net/vim-plug/ale',
    })

    use({
      'https://gitcode.net/vim-plug/vim-easy-align',
    })

    use({
      'https://gitcode.net/vim-plug/vim-virtualenv',
    })


    use({
      'https://gitcode.net/vim-plug/vim-table-mode',
    })

    if packer_bootstrap then
      require('packer').sync()
    end
  end)
